<?php

class Shop_search extends CI_Controller{
    
    public function index(){    
        $this->load->model('Document_types_model');
        //$this->load->model('Supplier_model');
        $vars['typeDocuments'] = $this->Document_types_model->select_guide_shop();
       // $vars['supplier'] = $this->Supplier_model->getAll();

        $vars['searchsales'] = true;
        $vars['content_view'] = 'Shop_search';
        $vars['menu'] = 'shop';
         
        $this->load->view('template', $vars);
         
    }
    
     public function add($success_message = null) {
      //  $this->load->model('Supplier_model');
        
        $this->load->model('Document_types_model');
        $vars['typeDocuments'] = $this->Document_types_model->select_guide_shop();
        
        if (isset($success_message)) {
            $vars['success_message'] = true;
        }
      //  $vars['supplier'] = $this->Supplier_model->getAll();
        $vars['content_view'] = 'Shop_search';
        $vars['menu'] = 'shop';
 
        $this->load->view('template', $vars);
    }
    
    
     public function shop() {


     	$supplier=null;
        $code = null;
        $date = null;
        $obtservations=null;
        $this->load->model('Products_model');
        if ($this->input->post('id_supplier')) {
        	$supplier= $this->input->post('id_supplier');       }
        

        if ($this->input->post('document_code')) {
        	$code = $this->input->post('document_code');
        }
        if ($this->input->post('date')) {
            $date = $this->input->post('date');
        }
        if($this->input->post('obtservations')){
        	$obtservations= $this->input->post('obtservations');
        }
        $shop = array();
        if ($supplier != null || $code != null || $date != null|| $obtservations != null) {
            $shop = $this->Products_model->search($code, $description);
        }
        $vars['shop'] = $shop;
        $vars['id_supplier'] = $supplier;
        $vars['document_code'] = $code;
        $vars['date'] = $date;
        $vars['obtservations'] = $obtservations;
        $this->load->view('shop_search', $vars);
    }
    
     public function stocks() {
        $code = null;
        $description = null;
        if ($this->input->post('code')) {
            $code = $this->input->post('code');
        }
        if ($this->input->post('description')) {
            $description = $this->input->post('description');
        }
        
        $this->load->model('warehouses_model');
	$warehouses = $this->warehouses_model->getAll();

        $this->load->model('Warehouses_product_model');
        $this->load->model('Products_model');
        $listStock = $this->Warehouses_product_model->listStockByProduct($code);
        $productData = $this->Products_model->find($code);
        $vars['priceProduct'] = $productData->price;
        $vars['codeProduct'] = $code;
        $vars['descriptionProduct'] = $description;
        $vars['listStock'] = $listStock;

        $vars['warehouses'] = $warehouses;
        $vars['productData'] = $productData;
        $this->load->view('stock_list_shop', $vars);
    }
	
	public function submit(){
           $this->load->model('Shop_model');
           $this->load->model('Warehouses_product_model');
           
           $contenaumentador = $this->input->post('contador_detalles');
                 
            $data = array('document_type_id' => 4,
                        'document_code' => $this->input->post('numero'),
                        'date' => $this->input->post('date'),
                 //       'id_supplier' =>  $this->input->post('supplier'),
                        'obtservations' => $this->input->post('observations'),
                        't_change' => $this->input->post('t_change'),
                        'igv' => $this->input->post('igv'),
                        'money' =>  $this->input->post('type_money'),
                        'utilidad' => $this->input->post('utilidad'),
                        'otros_costos' => $this->input->post('costo'),
                        'amount' => $this->input->post('subtotalgeneral')
                        );
       
           $this->Shop_model->add_shop($data);
       for($i = 1 ; $i <= $contenaumentador; $i++){
               
               $data = array('document_code' => $this->input->post('numero'),
                             'product_code' => $this->input->post('code_product_'.$i.''),
                             'box' => $this->input->post('total_cajas_'.$i.''),  
                             'pack' => $this->input->post('total_paquetes_'.$i.''),  
                             'unit' => $this->input->post('total_unidades_'.$i.''),  
                             'flete' => $this->input->post('total_flete_'.$i.''),  
                             'seguro' => $this->input->post('total_seguro_'.$i.''), 
                             'Ad_Val' => $this->input->post('total_adval_'.$i.''), 
                             'Val_Unit' => $this->input->post('total_price_'.$i.''), 
                             'amount' => $this->input->post('total_subtotalprice_'.$i.''), 
                            );
                $this->Shop_model->add_shop_dateils($data);
               
            for ($j = 1; $j <= 7; $j++){
                
                 $code_product = $this->input->post('code_product_'.$i);
                 $code_almacen = $this->input->post('code_almacen_'.$j.'_fila_'.$i);
                
                 $productos_en_alamcenes = $this->Shop_model->select_almacen_productos($code_product,$code_almacen);
                 
                
                echo "caja almacen".$j." fila ".$i.": ".$this->input->post('caja_almacen_'.$j.'_fila_'.$i)."<br>";
                echo "paquete almacen".$j." fila ".$i.": ".$this->input->post('caja_almacen_'.$j.'_fila_'.$i)."<br>";
                echo "unidad almacen".$j." fila ".$i.": ".$this->input->post('caja_almacen_'.$j.'_fila_'.$i)."<br><br><br>";
                 
                 if(count($productos_en_alamcenes)<=0){
                    
                   $data = array('id_warehouse' => $code_almacen, 'id_product' => $code_product,'Units' => $this->input->post('unidad_almacen_'.$j.'_fila_'.$i), 'packs' => $this->input->post('paquete_almacen_'.$j.'_fila_'.$i),'boxes' => $this->input->post('caja_almacen_'.$j.'_fila_'.$i) );
                   $this->Warehouses_product_model->add($data);                                         
                     
                 }else{
                    
                     foreach($productos_en_alamcenes as $pa){
                         $pa->boxes; 
                         $pa->packs;
                         $pa->Units;
                     }
                     
                 }
               
                 
            }
               
               
           }    
    	
	}
        
        
     public function codeguia() {
        $this->load->model('Document_types_model');
        $codeguia = $this->Document_types_model->generanum_shop();

        foreach ($codeguia as $c) {
            $codestring = $c->document_code;
            $num = (int) $codestring + 1;
            echo str_pad((int) $num, 12, "0", STR_PAD_LEFT);
        }
    }
        
      public function search_code_unique() {
        $this->load->model('Shop_model');
        $document_code = $this->input->post('numero');

        echo $this->Shop_model->comparaction_code($document_code);
        return false;
       
      }
    
}
?>
